package com.tang.usercenter.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class FileUtil {

    public String readUtil(File sourceFile){
	
	try
	{
		return this.readFile(new FileInputStream(sourceFile));
	} catch (FileNotFoundException e)
	{
		ConstantFinalUtil.LOGGER.error("文件不存在;文件路径:{}",sourceFile,e);
	}
	return "" ;
    }

    public String readFile(InputStream is) {
	StringBuffer sb = new StringBuffer();
	BufferedReader br = null ;
	try
	{
		br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
		String line = "" ; 
		while((line = br.readLine()) != null)
		{
			//System.out.println(line);
			/* 处理注释 */
			line = line.trim() ;
			if("".equalsIgnoreCase(line) || line.startsWith("//"))
			{
				continue ; 
			}
			
			sb.append(line);
		}
	}catch (Exception e)
	{
		ConstantFinalUtil.LOGGER.error("读取输入流内容失败了:",e);
	}finally
	{
		try
		{
			if(br != null)
			{
				br.close();
			}
		} catch (IOException e)
		{
			ConstantFinalUtil.LOGGER.error("读取输入流内容关闭时失败了:",e);
		}
	}
	return sb.toString();
    }
}
